import Vue from 'vue'
import VueRouter from 'vue-router'
import staticRoute from './staticRoute'
import {UserStatus} from "../network/userApi";

Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: staticRoute,
});

router.beforeEach((to, from, next) => {

    UserStatus({showMessage: 1}).then(res => {
        if (res.code !== 2000 && to.path === "/screen") {
            next(
                router.replace({
                    path: "/login",
                    query: {message: "screen"}
                })
            )
        }
        if (res.code !== 2000 && !res && to.path !== "/screen" && to.path !== "/login"
            && to.path !== "/UserReg" && to.path !== "/board") {
            next(
                router.replace({
                    path: "/login",
                })
            )
        }
        if (res.code === 2000 && to.path === "/login") {
            next(
                router.push({
                    path: "/home"
                })
            )
        }
    }).catch(err => {
        console.log(err)
    })

    next()
})

export default router
